const mongoose = require('mongoose');

const topCategorySchema = new mongoose.Schema({
  name: {
    type: String,
    required: true,
    unique: true,
    trim: true
  },
}, {
  timestamps: true,
  toJSON: { virtuals: true },
  toObject: { virtuals: true }
});

// 虚拟字段：关联的小分类（子分类）
topCategorySchema.virtual('children', {
  ref: 'SubCategory',
  localField: '_id',
  foreignField: 'parent',
  justOne: false
});

const TopCategory = mongoose.model('TopCategory', topCategorySchema);

module.exports = TopCategory;